package br.com.ffm.praticas.facade;


import java.sql.SQLException;
import java.util.List;

import br.com.ffm.praticas.bean.Product;
import br.com.ffm.praticas.dao.ProductDAO;

public class ProductFACADE {
	
	private static ProductFACADE productFACADE;
	private static ProductDAO productDAO;
	
	private ProductFACADE() {
		productDAO = new ProductDAO();
	}
	
	/**
	 * Method that returns a unique instance of ProductFACADE
	 * @return Returns a Instance of ProductFACADE
	 */
	public static ProductFACADE getInstance(){
		if (productFACADE == null)
			productFACADE = new ProductFACADE();
		return productFACADE;
	}
	
	/**
	 * Method that tests if the Product already exist
	 * @return Return 'false' if Product already exist
	 * @throws SQLException 
	 */
	private boolean alreadyExistProduct(Product product) throws SQLException{
		if (product != null) {
			Product p = productDAO.getProduct(product.getId());
			if (p != null)
				return true;
			else
				return false;
		}
		return false;
	}
	
	/**
	 * Method that adds a Product  	
	 * @param product Product
	 * @return false if product already exist 
	 * @throws SQLException 
	 */
	public boolean insert(Product product) throws SQLException {
		if (!alreadyExistProduct(product)) {
			productDAO.insert(product);
			return true;
		}
		return false;
	}
	
	/**
	 * Method that removes a Product
	 * @param product Product
	 * @return false if product don't exist
	 */
	public boolean delete(Product product) {
		return productDAO.delete(product);
	}
	
	/**
	 * Method that return all the Products
	 * @return List<Product>
	 * @throws SQLException
	 */
	public List<Product> getProducts() throws SQLException {
		return productDAO.getProducts();
	}
	
	/**
	 * Method that looks for a Product
	 * @param id Product Identifier
	 * @return null if product don't exist
	 * @throws SQLException 
	 */
	public Product getProduct(int id) throws SQLException {
		return productDAO.getProduct(id);
	}
	
	/**
	 * Method that looks for a Product
	 * @param productName Product Name
	 * @return null if product don't exist
	 * @throws SQLException 
	 */
	public Product getProductByName(String productName) throws SQLException {
		return productDAO.getProductForName(productName);
	}
	
	/**
	 * Method that looks for a Product
	 * @param barCode Product Bar Code
	 * @return null if product don't exist
	 * @throws SQLException 
	 */
	public Product getProductByBarCode(String barCode) throws SQLException {
		return productDAO.getProductForBarCode(barCode);
	}
	
	/**
	 * Method that updates a Product
	 * @param product Product
	 * @return false if product don't exist
	 */
	public boolean update(Product product) {
		return productDAO.update(product);
	}

	/**
	 * Method that count the products list size
	 * @return Size of products list
	 * @throws SQLException 
	 */
	public int count() throws SQLException {
		return productDAO.getProducts().size();
	}

}
